//
//  GRWebViewController.m
//  GroupCost_IOS
//
//  Created by apple on 2019/2/21.
//  Copyright © 2019年 apple. All rights reserved.
//

#import "GRWebViewController.h"
#import <Masonry/Masonry.h>
@interface GRWebViewController ()

/**
 关闭按钮
 */
@property (strong, nonatomic) UIButton *closeButton;

/**
 返回按钮
 */
@property (strong, nonatomic) UIButton *backButton;


@end

@implementation GRWebViewController
- (BOOL)prefersStatusBarHidden {
    return YES;
}
- (void)viewDidLoad {
	[super viewDidLoad];
	self.edgesForExtendedLayout = UIRectEdgeBottom;
}

- (void)viewDidAppear:(BOOL)animated{
	[super viewDidAppear:animated];
}

- (void)viewDidDisappear:(BOOL)animated{
	[super viewDidDisappear:animated];
}

- (void)dealloc {
#if DEBUG
	NSLog(@"%s",__FUNCTION__);
#endif
}

- (instancetype)initWithURLStr:(NSString *)urlstr{
	self = [super init];
	if (self) {
		self.urlstr = urlstr;
		[self.webContainer loadURLstr:urlstr];
	}
	return self;
}

- (instancetype)initWithURL:(NSURL *)URL{
	self = [super init];
	if (self) {
		self.URL = URL;
		[self.webContainer loadURL:URL];
	}
	return self;
}

- (instancetype)initWithFileName:(NSString *)fileName{
	self = [super init];
	if (self) {
		self.fileName = fileName;
		[self.webContainer loadLocalHtmlFileName:fileName];
	}
	return self;
}

- (void)reloadFreshURL:(NSString *)url{
	[self.webContainer loadURL:[NSURL URLWithString:url]];
}

- (void)layoutNavigation {
	UIBarButtonItem *closeItem = [[UIBarButtonItem alloc] initWithCustomView:self.closeButton];
	UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:self.backButton];
	self.navigationItem.leftBarButtonItems = @[backItem, closeItem];
	
	__weak typeof(self) weakself = self;
	self.webContainer.titleDidChangeBlock = ^(NSString *title) {
		weakself.title = title;
	};
}

- (void)updateLayoutConstraints{
	[self.webContainer mas_makeConstraints:^(MASConstraintMaker *make) {
		make.edges.equalTo(self.view);
	}];
}

- (void)setupViews{
	[self.view addSubview:self.webContainer];
}

- (void)bindViewModel{
    self.view.backgroundColor = [UIColor colorWithRed: 246/255.0 green: 154/255.0 blue: 17/255.0 alpha:1.000];
}

#pragma mark -
- (void)closeButtonClick:(UIButton *)button {
	[self.navigationController popViewControllerAnimated:YES];
}

- (void)backButtonClick:(UIButton *)button {
	self.webContainer.webView.canGoBack ? self.webContainer.webView.goBack : [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 懒加载
- (UIButton *)closeButton{
	if (!_closeButton) {
		_closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
		_closeButton.frame = CGRectMake(0, 0, 37, 30);
		[_closeButton setTitle:@"关闭" forState:UIControlStateNormal];
		[_closeButton addTarget:self
						 action:@selector(closeButtonClick:)
			   forControlEvents:UIControlEventTouchUpInside];	}
	return _closeButton;
}

- (UIButton *)backButton{
	if (!_backButton) {
		_backButton = [UIButton buttonWithType:UIButtonTypeCustom];
		_backButton.frame = CGRectMake(0, 0, 37, 30);
		[_backButton setTitle:@"返回" forState:UIControlStateNormal];
		[_backButton addTarget:self
						action:@selector(backButtonClick:)
			  forControlEvents:UIControlEventTouchUpInside];
	}
	return _backButton;
}

- (GRWebViewContainer *)webContainer{
	if (!_webContainer) {
		_webContainer = [[GRWebViewContainer alloc] init];
	}
	return _webContainer;
}

@end
